/**
 * 
 */
package de.cbf.cam.dsa.tavern.carte.controller;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import de.cbf.cam.dsa.tavern.carte.exceptions.WrongTypeProvidedException;
import de.cbf.cam.dsa.tavern.carte.model.entities.Processing;
import de.cbf.cam.dsa.tavern.carte.model.entities.interfaces.IEditableEntity;
import de.cbf.cam.dsa.tavern.carte.ui.CompositeEditProcessings;
import de.cbf.cam.dsa.tavern.carte.ui.interfaces.ICompositeEditEntities;

/**
 * @author Stefan Alter
 *
 */
public class ControllerEditProcessings extends ControllerEditEntities {

	/**
	 * 
	 */
	public ControllerEditProcessings() {		
	}

	/* (non-Javadoc)
	 * @see de.cbf.cam.dsa.tavern.carte.controller.ControllerEditEntities#checkEntity(de.cbf.cam.dsa.tavern.carte.model.entities.interfaces.IEditableEntity)
	 */
	@Override
	protected boolean checkEntity(IEditableEntity value)
			throws IllegalArgumentException {
		if (value instanceof Processing) {
			return true;			
		}if(value == null){
			throw new WrongTypeProvidedException(Processing.class.getName(),"null");	
		}
		throw new WrongTypeProvidedException(Processing.class, value.getClass());			
	}

	/* (non-Javadoc)
	 * @see de.cbf.cam.dsa.tavern.carte.controller.ControllerEditEntities#checkView(de.cbf.cam.dsa.tavern.carte.ui.interfaces.ICompositeEditEntities)
	 */
	@Override
	protected boolean checkView(ICompositeEditEntities value)
			throws IllegalArgumentException {
		if (value instanceof CompositeEditProcessings) {			
			return true;
		}
		if(value == null){
			throw new WrongTypeProvidedException(CompositeEditProcessings.class.getName(),"null");	
		}
		throw new WrongTypeProvidedException(CompositeEditProcessings.class, value.getClass());	
	}

	/* (non-Javadoc)
	 * @see de.cbf.cam.dsa.tavern.carte.controller.ControllerEditEntities#getEntityChangedListener()
	 */
	@Override
	protected PropertyChangeListener getEntitiesChangedListener() {
		return pclProcessings;
	}

	/* (non-Javadoc)
	 * @see de.cbf.cam.dsa.tavern.carte.controller.ControllerEditEntities#getNewEntity()
	 */
	@Override
	protected IEditableEntity getNewEntity() {
		return new Processing();
	}
	
	public Collection<Processing> getEditedProcessings(){
		List<Processing> resultList = new ArrayList<Processing>();
		Iterator<IEditableEntity> iterator = entities.iterator();
		while (iterator.hasNext()) {
			IEditableEntity iEditableEntity = (IEditableEntity) iterator.next();
			if (iEditableEntity instanceof Processing) {
				Processing processing = (Processing) iEditableEntity;
				resultList.add(processing);
			}
		}		
		return resultList;
	}
	
	private PCLProcessings pclProcessings = new PCLProcessings(); 
	private class PCLProcessings implements PropertyChangeListener{
		@Override
		public void propertyChange(PropertyChangeEvent arg0) {
			view.setEntities(entities.iterator());			
		}	
	}
	/* (non-Javadoc)
	 * @see de.cbf.cam.dsa.tavern.carte.controller.ControllerEditEntities#doNewEntity()
	 */
	@Override
	public IEditableEntity doNewEntity() {
		IEditableEntity entity =  super.doNewEntity();
		doSelectEntity(entity);
		return entity;
	}
}
